package com.jf.ai.service;

import com.jf.ai.bean.Actor;
import com.jf.ai.bean.Actress;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.Generation;
import org.springframework.ai.chat.prompt.PromptTemplate;
import org.springframework.ai.converter.BeanOutputConverter;
import org.springframework.ai.deepseek.DeepSeekChatModel;
import org.springframework.ai.template.st.StTemplateRenderer;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.Objects;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User:22954
 * Date:2025-06-12
 * Time:20:23
 */
@Service
public class BeanStructService {
    private final ChatClient chatClient;
    private final DeepSeekChatModel deepSeekChatModel;

    public BeanStructService(ChatClient.Builder builder, DeepSeekChatModel deepSeekChatModel) {
        this.chatClient = builder.build();
        this.deepSeekChatModel = deepSeekChatModel;
    }

    public Actor getActorChatClient(String actorName) {
        Actor actor = chatClient.prompt()
                .user(u -> u.text("Generate the filmography of 5 movies for {actor}.")
                        .param("actor", actorName))
                .call()
                .entity(Actor.class);
        return actor;
    }

    public Actor getActorByChatModel(String actorName) {
        BeanOutputConverter<Actor> beanOutputConverter =
                new BeanOutputConverter<>(Actor.class);

        String format = beanOutputConverter.getFormat();


        String template = """
                Generate the filmography of 5 movies for {actor}.
                {format}
                """;
        StTemplateRenderer stTemplateRenderer = StTemplateRenderer.builder().startDelimiterToken('{').endDelimiterToken('}').build();

        Generation generation = deepSeekChatModel.call(
                PromptTemplate.builder().template(template).variables(Map.of("actor", actorName, "format", format)).renderer(stTemplateRenderer).build().create())
                .getResult();

        Actor actor = beanOutputConverter.convert(Objects.requireNonNull(generation.getOutput().getText()));
        return actor;
    }
    public Actress getActressChatClient(String actressName) {
        Actress actress = chatClient.prompt()
                .user(u -> u.text("Generate the filmography of 5 movies for {actress}.")
                        .param("actress", actressName))
                .call()
                .entity(Actress.class);
        return actress;
    }
    public Actress getActressByChatModel(String actressName) {
        BeanOutputConverter<Actress> beanOutputConverter =
                new BeanOutputConverter<>(Actress.class);

        String format = beanOutputConverter.getFormat();


        String template = """
                Generate the filmography of 5 movies for {actor}.
                {format}
                """;

        Generation generation = deepSeekChatModel.call(
                        PromptTemplate.builder().template(template).variables(Map.of("actor", actressName, "format", format)).build().create())
                .getResult();

        Actress actress = beanOutputConverter.convert(Objects.requireNonNull(generation.getOutput().getText()));
        return actress;
    }
}
